package com.neperia.migratore;

import java.util.ArrayList;
import java.util.Iterator;

public class DataBase {

	public String nome_db;
	public int length=0;
	public Table[] tab;

	public DataBase(Riga[] db){
		length=db.length;
		nome_db=db[0].nome_db;
		ArrayList<Riga> r = new ArrayList<Riga>();
		if(db.length>0){
			r.add(db[0]);
			for(int i=1;i<db.length;i++){
				if(!db[i].nome_tabella.equals(db[i-1].nome_tabella)){
					r.add(db[i]);
				}
			}
		}
		Iterator<Riga> it = r.iterator();
		tab = new Table[r.size()];
		int i=0;
		while(it.hasNext()){
			Riga el = it.next();
			tab[i]=createTable(el,db);
			i++;
		}


	}

	private Table createTable(Riga el, Riga[] db) {
		// TODO Auto-generated method stub
		Table tab =new Table(el.nome_tabella,el.len_tabella,el.nome_db);
		ArrayList<Key> k = new ArrayList<Key>();
		if(db.length>0){
			if(el.nome_tabella.equals(db[0].nome_tabella)){
				k.add(new Key(db[0].nome_chiave,db[0].len_chiave,db[0].type_chiave,db[0].chiave_univoca,getParent(db[0].parent,db)));
			}
			for(int i=1;i<db.length;i++){
				if(el.nome_tabella.equals(db[i].nome_tabella) && !db[i].nome_chiave.equals(db[i-1].nome_chiave)){
					k.add(new Key(db[i].nome_chiave,db[i].len_chiave,db[i].type_chiave,db[i].chiave_univoca,getParent(db[i].parent,db)));//TODO controllare differenza nome kiave			
				}
			}
		}
		Iterator<Key> it = k.iterator();
		Key[] chiavi=new Key[k.size()];
		int n=0;
		while(it.hasNext()){
			Key c = it.next();
			chiavi[n]=c;
			chiavi[n].setValues(c.name,db);
			if(chiavi[n].parent!=null){
				if(chiavi[n].parent.length>0){
					tab.isRoot=false;
					chiavi[n].setGenitori(setParent(chiavi[n].parent,db));
				}
			}else{
				tab.isRoot=true;
			}
			n++;
		}
		tab.setKey(chiavi);
		return tab;
	}



	private Parent[] setParent(String[] parent,Riga[] db){
		ArrayList<Parent> p = new ArrayList<Parent>();
		//System.out.print(parent.length+": ");
		if(parent.length>0){
			for(int i=0;i<parent.length;i++){
				p.add(new Parent(parent[i], db));
			}
		}

		Iterator<Parent> it = p.iterator();
		Parent[] genitori = new Parent[parent.length];
		int c=0;
		while(it.hasNext()){
			genitori[c]=it.next();
			c++;
		}
		//System.out.println("!");
		return genitori;
	}


	private String[] getParent(String parent, Riga[] db) {
		// TODO Auto-generated method stub
		if(!parent.equals("")){
			ArrayList<String> s = new ArrayList<String>();

			getAll(s,parent,db);

			Iterator<String> it = s.iterator();
			if(s.size()>0){
				String[] genitori = new String[s.size()];
				int c=0;
				while(it.hasNext()){
					genitori[c]=it.next();
					c++;
					//System.out.print(genitori[c]+", ");
				}
				//System.out.println("!");

				return genitori;
			}else{
				return null;
			}
		}else{
			return null;
		}
	}

	private void getAll(ArrayList<String> s, String parent, Riga[] db) {
		// TODO Auto-generated method stub
		//if(!parent.equals("")){
		for(int i=0;i<db.length;i++){
			if(db[i].nome_tabella.equals(parent)){
				s.add(db[i].nome_tabella);
				if(!db[i].parent.equals("") && db[i].parent!=null){
					getAll(s, db[i].parent, db);
				}
			}
		}
		//}
	}

}
